import {routes} from "@/router";
import router from "@/router";
import defaultSettings from '@/settings'
const Layout = () => import('@/components/Layout/index')
const Empty = () => import('@/components/Layout/Empty')

import user from './user'

function filterSYs(sysList) {
  let res = []
  for (let i = 0; i < sysList.length; i++) {
    if (sysList[i].route) {
      if (sysList[i].jumpbaseurl == process.env.VUE_APP_BASE_URL) {
        // 使用本地路由文件
        if (defaultSettings.isUseLocalRoute) {
          // 是否根据权限过滤本地路由
          if (defaultSettings.isJudgeLocalRoutePerm) {
            const localRoutes = hasPermission(routes, sysList[i].route)
            if (localRoutes.length <= 1) {
              sysList[i].route = []
            } else {
              sysList[i].route = routes
            }
          } else {
            sysList[i].route = routes
          }
        }
      }
      res = res.concat(filterRoutes(sysList[i].route, sysList[i].code, sysList[i].baseurl, sysList[i].jumpbaseurl))
    }
  }
  return res
}

function filterRoutes(routeList, code, baseurl, jumpbaseurl) {
  let res = [];
  let iframeRegx = /(http|https):\/\/([\w.]+\/?)\S*!/
  routeList.forEach(route => {
    let iframeUrl = iframeRegx.test(route.iframepath) ? route.iframepath : baseurl + route.iframepath;
    let path = ''
    let title = ''
    let icon = ''
    if (process.env.VUE_APP_BASE_URL !== jumpbaseurl) {
      title += route.name
      path += jumpbaseurl + route.fullpath
      icon += route.icon ? 'iconfont  ' + route.icon : ''
    } else {
      if (defaultSettings.isUseLocalRoute) {
        title += route.meta ? route.meta.title : ''
        icon += route.meta ? route.meta.icon : ''
      } else {
        title += route.name
        icon += route.icon ? 'iconfont  ' + route.icon : ''
      }
      path = route.path
    }
    let item = {
      path: path,
      redirect: route.redirect,
      meta: {
        title: title,
        icon: icon,
        activeMenu: route.activemenu,
        sys: code,
        iframeurl: iframeUrl,
        jumpbaseurl: jumpbaseurl
      }
    };
    !!route.hidden && (item.hidden = true)
    !!route.alwaysshow && (item.alwaysShow = true)
    !!route.nocache && (item.meta.noCache = true)
    !!route.affix && (item.meta.affix = true)
    !!route.breadcrumb && (item.breadcrumb = true)
    if (route.component) {
      if (route.component === 'Layout') {
        item.component = Layout
      } else if (route.component === 'Empty') {
        item.component = Empty
      } else {
        item.component = () => import('@/views/' + route.component.replace(/_/g, '/'))
      }
    }
    if (route.children) {
      item.children = filterRoutes(route.children, code, baseurl, jumpbaseurl)
    }
    res.push(item)
  })
  return res
}

export function hasPermission(localRoutes, sysRoutes) {
  for (let i = 0; i < localRoutes.length; i++) {
    const index = sysRoutes.findIndex(d => d.path === localRoutes[i].path)
    if (index == -1) {
      if (!localRoutes[i].hidden) {
        if (router.hasRoute(localRoutes[i].name)) {
          router.removeRoute(localRoutes[i].name)
        }
        localRoutes.splice(i, 1)
        i--
      }
    } else {
      if (localRoutes[i].children && sysRoutes[index].children) {
        hasPermission(localRoutes[i].children, sysRoutes[index].children)
      }
    }
  }
  return localRoutes
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = routes
  }
}

const actions = {
  generateRoutes({ commit }) {
    return new Promise((resolve) => {
      let consoleRoutes = []
      if (defaultSettings.isOnlyLocalRoute) {
        consoleRoutes = routes
      } else {
        consoleRoutes = filterSYs(user.state.syslist)
      }
      consoleRoutes.push({path: '/:pathMatch(.*)*', redirect: '/noPerm', hidden: true})
      commit('SET_ROUTES', consoleRoutes)
      resolve(consoleRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

